package Java.view.game.douniu;

import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;

import java.awt.*;

public class Poker extends StackPane {

    private douniuFX gameFX;
    private String name;
    private int huaSe;
    private int value;
    private boolean up;
    private boolean canClick = false;
    private boolean clicked = false;
    private ImageView imageView;

    public Poker(douniuFX gameFX, String name, int value, boolean up) {
        this.gameFX = gameFX;
        this.name = name;
        this.value = value % 13 + 1;
        this.huaSe = value / 13;
        if (this.value > 10) {
            this.value = 10;
        }
        this.up = up;

        imageView = new ImageView();
        setImage();

        this.getChildren().add(imageView);
        this.setOnMouseClicked(this::handleMouseClick);

        this.setPrefSize(110, 150);
        this.setVisible(true);
    }
    public Poker(String name, int value, boolean up) {
        this.name = name;
        this.value = value % 13 + 1;
        this.huaSe = value / 13;
        if (this.value > 10) {
            this.value = 10;
        }
        this.up = up;

        imageView = new ImageView();
        setImage();

        this.getChildren().add(imageView);
        this.setOnMouseClicked(this::handleMouseClick);

        this.setPrefSize(110, 150);
        this.setVisible(true);
    }
    public void setImage() {
        Platform.runLater(() -> {
            try {
                if (this.up) {
                    imageView.setImage(new Image("file:image/" + name + ".jpg"));
                } else {
                    imageView.setImage(new Image("file:image/rear.png"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    private void handleMouseClick(MouseEvent event) {
        if (canClick) {
            clicked = !clicked;
            double step = clicked ? -20 : 20;
            this.setTranslateY(this.getTranslateY() + step);
        }
        System.out.println("点击了扑克牌：" + name + ",面值:" + value + ",花色:" + huaSe + "是否被点击：" + clicked);
    }

    // Getters and Setters
    public douniuFX getGameFX() {
        return gameFX;
    }

    public void setGameFX(douniuFX gameFX) {
        this.gameFX = gameFX;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
        setImage();
    }

    public int getHuaSe() {
        return huaSe;
    }

    public void setHuaSe(int huaSe) {
        this.huaSe = huaSe;
    }

    public ImageView getImageView() {
        return imageView;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public boolean isCanClick() {
        return canClick;
    }

    public void setCanClick(boolean canClick) {
        this.canClick = canClick;
    }

    public boolean isClicked() {
        return clicked;
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    @Override
    public String toString() {
        return "Poker{" +
                "gameFX=" + gameFX +
                ", name='" + name + '\'' +
                ", huaSe=" + huaSe +
                ", value=" + value +
                ", up=" + up +
                ", canClick=" + canClick +
                ", clicked=" + clicked +
                '}';
    }
    //获得坐标
    public Point getLocation() {
        return new Point((int) this.getLayoutX(), (int) this.getLayoutY());
    }
    //显示牌的正面
    public void turnFront() {
        this.setUp(true);
        setImage();
    }
    //通过name获得牌
    public static Poker getPokerByName(String name) {
        return null;
    }
}